package com.hj.chartdemo.chart;

import android.content.Context;

import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet;

import java.util.ArrayList;

public class BarChartManagger {

  private static BarChartManagger instance = null;

  public static BarChartManagger getInstance() {
    if (instance == null) {
      instance = new BarChartManagger();
    }
    return instance;
  }

  // 初始化
  public void init(Context context, BarChart barChart) {

    // 柱状图的阴影
    barChart.setDrawBarShadow(false);
    // 设置柱状图Value值显示在柱状图上方 true 为显示上方，默认false value值显示在柱状图里面
    barChart.setDrawValueAboveBar(true);
    // Description Label 是否可见
    barChart.getDescription().setEnabled(false);
    // 二指控制X轴Y轴同时放大
    barChart.setPinchZoom(false);
    // 是否显示表格背景颜色
    barChart.setDrawGridBackground(false);
    barChart.setDragEnabled(true);
    barChart.setScaleEnabled(true);

    XAxis xAxis = barChart.getXAxis();
    // 设置X轴显示位置
    xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
    //    xAxis.setTypeface(tfLight)
    // X轴纵向分割线，一般不设置显示
    xAxis.setDrawGridLines(false);
    xAxis.setLabelRotationAngle(60);
    // X轴显示Value值的精度，与自定义X轴返回的Value值精度一致
    xAxis.setGranularity(1);
    xAxis.setAxisMinimum(0.5f);
    //    xAxis.setValueFormatter(xAxisFormatter)

    //        val custom = MyAxisValueFormatter()

    YAxis leftAxis = barChart.getAxisLeft();
    //    leftAxis.setTypeface(tfLight)
    // 设置Y左边轴显示的值 label 数量
    //        leftAxis.setLabelCount(8, false)
    //        leftAxis.setValueFormatter(custom)
    // 设置值显示的位置，我们这里设置为显示在Y轴外面
    leftAxis.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART);
    leftAxis.setSpaceTop(30f);
    leftAxis.setAxisMinimum(0f);
    leftAxis.setGranularity(1f);

    YAxis rightAxis = barChart.getAxisRight();
    rightAxis.setEnabled(false);

    Legend l = barChart.getLegend();
    l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
    l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER);
    l.setOrientation(Legend.LegendOrientation.HORIZONTAL);
    l.setDrawInside(false);
    l.setForm(Legend.LegendForm.SQUARE);
    l.setFormSize(9f);
    l.setTextSize(11f);
    l.setXEntrySpace(4f);

    barChart.setNoDataText("暂无数据");

    barChart.setFitBars(true);
    barChart.animateY(700);
  }

  public void loadData(
      Context context,
      BarChart barChart,
      ArrayList<ArrayList<Entry>> datas,
      ArrayList<String> titles,
      ArrayList<Integer> colors,
      final ArrayList<String> xlab,
      Boolean showLegend) {

    barChart.getLegend().setEnabled(showLegend);

    BarDataSet set1 = null;

    if (barChart.getData() != null && barChart.getData().getDataSetCount() > 0) {
      //      set1 = (BarDataSet)barChart.getCrmData().getDataSetByIndex(0);
      //      set1.setValues(values);
      //      barChart.getCrmData().notifyDataChanged();
      //      barChart.notifyDataSetChanged();

      int count = barChart.getData().getDataSetCount();
      for (int i = 0; i < count; i++) {
        if (datas.size() - 1 >= i) {
          ArrayList<BarEntry> values = new ArrayList<BarEntry>();
          ArrayList<Entry> d = datas.get(i);
          for (int j = 0; j < d.size(); j++) {
            values.add(new BarEntry(d.get(j).getX(), d.get(j).getY(), d.get(j).getData()));
          }
          BarDataSet lineDataSet = (BarDataSet) barChart.getData().getDataSetByIndex(i);
          lineDataSet.setValues(values);
        }
      }
      barChart.getData().notifyDataChanged();
      barChart.notifyDataSetChanged();
    } else {
      //      set1.setColors(*ColorTemplate.VORDIPLOM_COLORS);
      //            set1.setHighLightAlpha(255)

      ArrayList<IBarDataSet> dataSets = new ArrayList<IBarDataSet>();
      for (int i = 0; i < datas.size(); i++) {
        ArrayList<BarEntry> values = new ArrayList<BarEntry>();
        ArrayList<Entry> d = datas.get(i);
        for (int j = 0; j < d.size(); j++) {
          values.add(new BarEntry(d.get(j).getX(), d.get(j).getY(), d.get(j).getData()));
        }
        String title = "";
        if (titles.size() - 1 >= i) {
          title = titles.get(i);
        }
        set1 = new BarDataSet(values, title);
        set1.setDrawIcons(false);
        if (colors.size() - 1 >= i) {
          set1.setColor(colors.get(i));
        }
        dataSets.add(set1);
      }
      if (xlab != null) {
        XAxis lineXAxis = barChart.getXAxis();
        lineXAxis.setValueFormatter(
            new IAxisValueFormatter() {
              @Override
              public String getFormattedValue(float value, AxisBase axis) {
                if ((int) (value - 1) >= xlab.size() || value == 0) {
                  return "";
                } else {
                  int tag = (int) (((value - 1) * 10 + 5) / 10);
                  return xlab.get(tag) + "";
                }
              }
            });
        // X轴的设置及自定义X轴
        BarAxisValueFormatter xAxisFormatter = new BarAxisValueFormatter(barChart, xlab);
        XYMarkerView mv = new XYMarkerView(context, xAxisFormatter);
        mv.setChartView(barChart); // For bounds control
        barChart.setMarker(mv); // Set the marker to the chart
      }

      BarData data = new BarData(dataSets);
      data.setValueTextSize(10f);
      data.setBarWidth(0.5f);

      barChart.setData(data);
      barChart.invalidate();
    }
  }
}
